package com.zoewin.zephyr.reportjasper.config;

import org.springframework.util.ObjectUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by WQL on 2019/12/17
 * CEASE TO STRUGGLE AND YOU CEASE TO LIVE
 */
public class JasperConfig {

    public volatile static String orgCharset = "utf-8";

    public volatile static String targetCharset = "utf-8";

    /**
     * 从容器里面获取所有的模板路径和图片路径
     */
    private ReportConfig reportConfig;

    /**
     * 需要用到的插入图片路径
     */
    private Map picPath = new HashMap();

    /**
     * 需要用到的报表jrxml路径
     */
    private String jrxmlPath;

    /**
     * 需要用到的报表jrxml路径
     */
    private String jasperPath;

    /**
     * 需要用到的报表jrxml路径
     */
    private String wordPath;

    /**
     * 获取报表jrxml的流
     */
    private InputStream inputStream;

    /**
     * 所有模板路径
     */
    private HashMap templatePaths = new HashMap();

    /**
     * 所有模板路径(编译后的模板文件)
     */
    private HashMap jasperPaths = new HashMap();

    /**
     * 所有模板路径(编译后的模板文件)
     */
    private HashMap wordPaths = new HashMap();

    /**
     * 所有图片路径
     */
    private HashMap picPaths = new HashMap();

    /**
     * jrxml模板名称
     */
    private String jrxmlName;

    /**
     * word格式模板名称
     */
    private String wordName;

    public JasperConfig() {
        try {
            WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
            reportConfig = wac.getBean(ReportConfig.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setPic(String str) throws Exception {
        Map map = null;
        try {
            map = reportConfig.getPicMap();
        } catch (Exception e) {
            map = new HashMap();
        }

        if (ObjectUtils.isEmpty(map.get(str))) {
            File file = null;
            try {
                file = ResourceUtils.getFile("classpath:jrxmlTemplate");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                throw new Exception("resources/jrxmlTemplate do not found");
            }
            getFilePath(file);
            picPath.put("url", picPaths.get(str));
        } else {
            picPath.put("url", map.get(str));
        }

    }

    public void setPics(String... strArr) throws Exception {
        Map map = null;
        try {
            map = reportConfig.getPicMap();
        } catch (Exception e) {
            map = new HashMap();
        }
        for (String key : strArr) {
            if (ObjectUtils.isEmpty(map.get(key))) {
                File file = null;
                try {
                    file = ResourceUtils.getFile("classpath:jrxmlTemplate");
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    throw new Exception("resources/jrxmlTemplate do not found");
                }
                getFilePath(file);
                picPath.put(key, picPaths.get(key));
            } else {
                picPath.put(key, map.get(key));
            }
        }

    }

    public void setJrxml(String str) throws Exception {
        this.jrxmlName = str;
        Map map = null;
        try {
            map = reportConfig.getTemplateMap();
        } catch (Exception e) {
            map = new HashMap();
        }
        if (ObjectUtils.isEmpty(map.get(str))) {
            File file = null;
            try {
                file = ResourceUtils.getFile("classpath:jrxmlTemplate");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                throw new Exception("resources/jrxmlTemplate do not found");
            }
            getFilePath(file);
            jrxmlPath = (String) templatePaths.get(str);
        } else {
            jrxmlPath = (String) map.get(str);
        }
    }

    public void setJasper(String str) throws Exception {
        this.jrxmlName = str;
        Map map = null;
        try {
            map = reportConfig.getJasperMap();
        } catch (Exception e) {
            map = new HashMap();
        }
        if (ObjectUtils.isEmpty(map.get(str))) {
            File file = null;
            try {
                file = ResourceUtils.getFile("classpath:jrxmlTemplate");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                throw new Exception("resources/jrxmlTemplate do not found");
            }
            getFilePath(file);
            jasperPath = (String) jasperPaths.get(str);
        } else {
            jasperPath = (String) map.get(str);
        }
    }

    public void setWord(String str) throws Exception {
        this.wordName = str;
        Map map = null;
        try {
            map = reportConfig.getWordMap();
        } catch (Exception e) {
            map = new HashMap();
        }
        if (ObjectUtils.isEmpty(map.get(str))) {
            File file = null;
            try {
                file = ResourceUtils.getFile("classpath:jrxmlTemplate");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                throw new Exception("resources/jrxmlTemplate do not found");
            }
            getFilePath(file, orgCharset, targetCharset);
            wordPath = (String) wordPaths.get(str);
        } else {
            wordPath = (String) map.get(str);
        }
    }


    public void reSetReportConfig(String jrxmlName, String... picName) throws Exception {
        if (!ObjectUtils.isEmpty(jrxmlName)) {
            reSetJrxml(JasperConfig.orgCharset, JasperConfig.targetCharset, jrxmlName);
        }
        if (!ObjectUtils.isEmpty(picName)) {
            reSetPics(JasperConfig.orgCharset, JasperConfig.targetCharset, picName);
        }
    }

    private void reSetJrxml(String orgCharset, String targetCharset, String str) throws Exception {
        this.jrxmlName = str;
        Map map = null;
        try {
            map = reportConfig.getTemplateMap();
        } catch (Exception e) {
            map = new HashMap();
        }
        if (ObjectUtils.isEmpty(map.get(str))) {
            File file = null;
            try {
                file = ResourceUtils.getFile("classpath:jrxmlTemplate");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                throw new Exception("resources/jrxmlTemplate do not found");
            }
            getFilePath(file, orgCharset, targetCharset);
            jrxmlPath = (String) templatePaths.get(str);
        } else {
            jrxmlPath = (String) map.get(str);
        }
    }

    private void reSetPics(String orgCharset, String targetCharset, String... strArr) throws Exception {
        Map map = null;
        try {
            map = reportConfig.getPicMap();
        } catch (Exception e) {
            map = new HashMap();
        }
        for (String key : strArr) {
            if (ObjectUtils.isEmpty(map.get(key))) {
                File file = null;
                try {
                    file = ResourceUtils.getFile("classpath:jrxmlTemplate");
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    throw new Exception("resources/jrxmlTemplate do not found");
                }
                getFilePath(file, orgCharset, targetCharset);
                picPath.put(key, picPaths.get(key));
            } else {
                picPath.put(key, map.get(key));
            }
        }
    }

    private void getFilePath(File file) {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (files != null) {
                for (File childFile : files) {
                    //递归所有的文件夹
                    getFilePath(childFile);
                }
            }
        } else {
            //当判断为文件的时候将文件名和文件的绝对路径进行保存放入容器
            String fileName = file.getName().substring(0, file.getName().lastIndexOf("."));
            String filePath = file.getAbsolutePath();
            if (filePath.contains("pics")) {
                this.picPaths.put(fileName, filePath);
            } else if (filePath.contains("jrxmls")) {
                this.templatePaths.put(fileName, filePath);
            } else if (filePath.contains("jaspers")) {
                jasperPaths.put(fileName, filePath);
            } else if (filePath.contains("words")) {
                wordPaths.put(fileName, filePath);
            }
        }
    }


    private void getFilePath(File file, String orgCharset, String targetCarset) throws Exception {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (files != null) {
                for (File childFile : files) {
                    //递归所有的文件夹
                    getFilePath(childFile, orgCharset, targetCarset);
                }
            }
        } else {
            //当判断为文件的时候将文件名和文件的绝对路径进行保存放入容器
            String fileName = file.getName().substring(0, new String(file.getName().getBytes(orgCharset), targetCarset).lastIndexOf("."));
            String filePath = new String(file.getAbsolutePath().getBytes(orgCharset), targetCarset);
            if (filePath.contains("pics")) {
                this.picPaths.put(fileName, filePath);
            } else if (filePath.contains("jrxmls")) {
                this.templatePaths.put(fileName, filePath);
            } else if (filePath.contains("jaspers")) {
                this.jasperPaths.put(fileName, filePath);
            } else if (filePath.contains("words")) {
                this.wordPaths.put(fileName, filePath);
            }
        }
    }

    public String getJrxmlName() {
        return this.jrxmlName;
    }

    public String getJrxmlPath() {
        return jrxmlPath;
    }

    public String getJasperPath() {
        return jasperPath;
    }

    public String getWordPath() {
        return wordPath;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public Map getPicPath() {
        return picPath;
    }

    public String getWordName() {
        return wordName;
    }
}

